package com.vint.lintcode;

import com.vint.common.ListNode;

/**
 * 求链表中倒数第n个节点， 容易
 */
public class Lintcode166 {
	public ListNode nthToLast(ListNode head, int n) {
		if (null == head || null == head.next) {
			return head;
		}
		int lenght = getLenght(head);
		ListNode visit = head;
		int len = 0;
		while (null != visit && len != lenght - n) {
			len++;
			visit = visit.next;
		}
		return visit;
	}
	public int getLenght(ListNode head) {
		ListNode visit = head;
		int lenght = 0;
		while (null != visit) {
			lenght++;
			visit = visit.next;
		}
		return lenght;
	}
}
